import axios from '@/request/request'
// 下载方法
export const Download = async (res) => {
  const res2 = await axios.get(res.data.url, { params: {}, responseType: 'blob' })
  const url = window.URL.createObjectURL(new Blob([res2.data]))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  const fileName = res.data.url.split('/')
  link.setAttribute('download', fileName[fileName.length - 1])
  document.body.append(link)
  link.click()
  link.remove()
}

// 搜索关键词高亮
export const lightKeyword = (keyword, result) => {
  const reg = new RegExp(`${keyword}`, 'g')
  for (let key in result) {
    if (key !== 'order') {
      result[key].forEach((item) => {
        item.sugName = item.name.replace(reg, `<span style="color:rgb(48, 136, 236)">${keyword}</span>`)
      })
    }
  }
  return result
}

// 格式化时间戳
export const FormatTime = (datetime) => {
  if (datetime.length == 10) datetime *= 1000
  //datetime是拿到的时间戳
  const date = new Date(datetime)//时间戳为10位需*1000，时间戳为13位的话不需乘1000 
  const year = date.getFullYear(),
    month = ("0" + (date.getMonth() + 1)).slice(-2),
    sdate = ("0" + date.getDate()).slice(-2)
  // 拼接
  const result = year + "-" + month + "-" + sdate
  // 返回
  return result
}